package erfgame.core;

import java.util.ArrayList;

public abstract class AbstractOperation<K, E extends Throwable> implements Operation<K, E> {
	private ArrayList<OperationProgressListener> progressListeners;
	
	private OperationProgress progress;
	
	public AbstractOperation(OperationProgress startingProgress) {
		this.progress = startingProgress;
		this.progressListeners = new ArrayList<OperationProgressListener>(1);
	}
	
	public void setProgress( OperationProgress progress ) {
		this.progress = progress;
		this.fireProgressEvent( new OperationProgressEvent(progress) );
	}
	
	public OperationProgress getProgress() {
		return progress;
	}

	protected void fireProgressEvent( OperationProgressEvent event ) {
		for( int i=this.progressListeners.size(); i>0; ) {
			i--;
			OperationProgressListener listener;
			listener = this.progressListeners.get( i );
			listener.progressUpdated(event);
		}
	}

	public void addOperationProgressListener(OperationProgressListener listener) {
		progressListeners.add(listener);
	}

	public void removeOperationProgressListener(
			OperationProgressListener listener) {
		progressListeners.remove(listener);
	}
}
